﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace My.Core.UnifyResult.Attributes
{
	/// <summary>
	/// 规范化结果配置
	/// </summary>
	public class UnifyResultAttribute : ProducesResponseTypeAttribute
	{
		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="statusCode"></param>
		public UnifyResultAttribute(int statusCode) : base(statusCode)
		{
		}

		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="type"></param>
		/// <param name="statusCode"></param>
		/// <param name="method"></param>
		internal UnifyResultAttribute(Type type, int statusCode, MethodInfo method) : base(type, statusCode)
		{
			WrapType(type, method);
		}

		/// <summary>
		/// 包装类型
		/// </summary>
		/// <param name="type"></param>
		/// <param name="method"></param>
		private void WrapType(Type type, MethodInfo method = default)
		{
			var unityMetadata = typeof(RESTfulResult<>);

			if (unityMetadata != null)
			{
				Type = unityMetadata.MakeGenericType(type);
			}
			else Type = default;
		}
	}
}
